/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author bennyl
 */
public class ImmutableCombinedCollectionView<T> implements Collection<T> {

    private List<Collection<T>> combination = new LinkedList<>();
    private int size;

    public ImmutableCombinedCollectionView(Collection<T>... combination) {
        size = 0;
        for (Collection<T> c : combination) {
            size += c.size();
            this.combination.add(c);
        }
    }

    public ImmutableCombinedCollectionView(Collection<Collection<T>> combination) {
        size = 0;
        for (Collection<T> c : combination) {
            size += c.size();
            this.combination.add(c);
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(Object o) {
        for (Collection<T> c : combination) {
            if (c.contains(o)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            Iterator<Collection<T>> it = combination.iterator();
            Iterator<T> curit = nextIterator();

            @Override
            public boolean hasNext() {
                return curit != null;
            }

            @Override
            public T next() {
                T next = curit.next();
                if (!curit.hasNext()) {
                    curit = nextIterator();
                }
                return next;
            }

            private Iterator<T> nextIterator() {
                Collection<T> res = null;
                while (it.hasNext() && (res = it.next()).isEmpty()) {
                    continue;
                }
                if (res == null || res.isEmpty()) {
                    return null;
                }
                return res.iterator();
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("immutable collection.");
            }
        };
    }

    @Override
    public Object[] toArray() {
        ArrayList<T> all = new ArrayList<>(size);
        for (Collection<T> c : combination) {
            all.addAll(c);
        }
        return all.toArray();
    }

    @Override
    public <E> E[] toArray(E[] a) {
        ArrayList<T> all = new ArrayList<>(size);
        for (Collection<T> c : combination) {
            all.addAll(c);
        }
        return all.toArray(a);
    }

    @Override
    public boolean add(T e) {
        throw new UnsupportedOperationException("immutable collection.");
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException("immutable collection.");
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        for (Object o : c) {
            if (!contains(o)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        throw new UnsupportedOperationException("immutable collection.");
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException("immutable collection.");
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException("immutable collection.");
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("immutable collection.");
    }

    @Override
    public String toString() {
        return new StringBuilder("CombinedCollection{").append(combination).append("}").toString();

    }
}
